home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / JANDELIA.C < prev    next >
C/C++ Source or Header  |  1993-06-10  |  5KB  |  130 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 06-07-93 (18:22)             Number: 165
  4. From: MARK CORGAN                  Refer#: NONE
  5.   To: ALL                           Recvd: NO  
  6. Subj: Optimization again...          Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. Hello All!
  9.  
  10. A few months ago I posted some code that generates the Mandelbrot fractal set in
  11.  the hopes that someone could speed up the algorithm. Someone, whose name I have
  12.  now forgotten, helped me very much and made the routine almost 50% faster!
  13. I have some more code that I would like optimized for speed. It generates the Ma
  14. ndelbrot fractal set as well as its correspnding Julia set. I have hard-coded th
  15. e parameters in the following code for test purposes only. Of course, they would
  16.  be user-selectable. I know I need to take out as many floating-point calculatio
  17. ns from within the for() loops but I do not have a profiler to determine where t
  18. he biggest bottlenecks are. Also, does anyone have a substitute for kbhit()? It
  19. is in one of the for() loops and it REALLY slows things down by continually chec
  20. king the keyboard through each loop iteration. I would appreciate any help. :)
  21.  
  22. /* JANDELIA.C
  23.  *
  24.  * A program to display Mandelbrot and Julia Sets.
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <conio.h>
  30. #include <graphics.h>
  31. #include <math.h>
  32. #include <time.h>
  33.  
  34. int niter;                /* maximum number of iterations */
  35. int px, py;               /* current pixel */
  36. int nx, ny;               /* number of pixels */
  37. int nc;                   /* number of colors to use */
  38. int i, j;                 /* counters for miscellaneous use */
  39. int keepon;               /* 1 = keep going, 0 = abort */
  40. int jul;                  /* 1 = Julia Set,  0 = Mandelbrot */
  41. float x, y;               /* last point on orbit */
  42. float xx, yy;             /* next point on orbit */
  43. float xsquared, ysquared; /* used to speed up computations */
  44. float dx, dy;             /* theoretical size of pixels */
  45. float cx, cy;             /* number to add each iteration */
  46. float xO, yO;             /* starting x,y */
  47. float xI, yI;             /* ending x,y */
  48.  
  49. void main(void)
  50. {
  51.    time_t start, end;
  52.  
  53.    /* request auto detection */
  54.    int gdriver = DETECT, gmode, errorcode;
  55.  
  56.    /* initialize graphics mode */
  57.    initgraph(&gdriver, &gmode, "\\tc\\bgi");
  58.  
  59.    /* read result of initialization */
  60.    errorcode = graphresult();
  61.  
  62.    if (errorcode != grOk)  /* an error occurred */
  63.    {
  64.       printf("Graphics error: %s\n", grapherrormsg(errorcode));
  65.       printf("Press any key to halt:");
  66.       getch();
  67.       exit(1);             /* return with error code */
  68.    }
  69.  
  70.    cx = 0.5;               /* hard-coded for testing */
  71.    cy = 0.5;               /* cx and cy are used only when jul = 1 */
  72.    xO = -2.0;
  73.    yO = 1.25;
  74.    xI = 1.0;
  75.    yI = -1.25;
  76.    niter = 16;
  77.    px = 0, py = 0;
  78.    nx = 200;
  79.    ny = 200;
  80.    nc = 16;
  81.    keepon = 1;
  82.    jul = 0;
  83.  
  84.    start = time(NULL);
  85.  
  86.    dx = (xI - xO) / nx;          /* compute size of pixels */
  87.    dy = (yI - yO) / ny;
  88.    for (px = 0; (px < nx) && (keepon); px++)  /* main loop */
  89.       for (py = 0; (py < ny) && (keepon); py++)
  90.       {
  91.          x = xO + px * dx,       /* start julia set on pixel */
  92.          y = yO + py * dy;
  93.          if (jul == 0)           /* start mandelbrot set on 0,0 */
  94.             cx = x, cy = y,
  95.          x = 0.0, y = 0.0;       /* and use pixel for c */
  96.          xsquared = 0, ysquared = 0;
  97.          for (i = 0; (i < niter) && (xsquared + ysquared < 4.0); i++)
  98.          {
  99.             xsquared = x * x;
  100.             ysquared = y * y;
  101.             xx = xsquared - ysquared + cx;
  102.             yy = cos(x * y * 2.0) + cy;
  103.             x = xx, y = yy;
  104.          }
  105.          if (i == niter) i = 0;  /* hit limit, color 0 */
  106.          else i = (i % 256);      /* color determined by i */
  107.          putpixel(px, py, i);    /* lite up the pixel */
  108.          if (kbhit() && (getch() == 27))
  109.             keepon = 0;         /* stop if user hit Esc */
  110.       }
  111.    end = time(NULL);
  112.    printf("\7");                 /* beep when done */
  113.    outtextxy(0, 300, "Press almost any key for completion time . . .");
  114.    getch();
  115.    restorecrtmode();
  116.    printf("Time for completion was %ld seconds.", end - start);
  117.    printf("\n\nPress almost any key to quit . . .");
  118.    getch();                      /* wait for a keypress */
  119.    closegraph();                 /* shut down graphics */
  120. }
  121.  
  122.  
  123. Mark
  124.  
  125. --- GoldED 2.40
  126.  * Origin: -*- Sound and Image -*-  (1:213/810)
  127. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  128. SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
  129. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  130.